package com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.basedata.progroup.bean.LimsGroupBean;
import com.sunwayworld.basemodule.basedata.progroup.service.LimsGroupService;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolplan.bean.LimsQualitycontrolplanBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolplan.bean.LimsQualitycontrolplanProjectBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolplan.service.LimsQualitycontrolplanProjectService;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolplan.service.LimsQualitycontrolplanService;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.bean.LimsQualitycontrolTestResultBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.bean.LimsQualitycontrolTestResultManageBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.dao.LimsQualitycontrolTestResultDao;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.service.LimsQualitycontrolTestResultManageService;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.service.LimsQualitycontrolTestResultService;
import com.sunwayworld.basemodule.business.qualitycontrol.qualityrule.bean.LimsQualityRuleBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualityrule.service.LimsQualityRuleService;
import com.sunwayworld.basemodule.common.editrecord.bean.LimsEditRecordBean;
import com.sunwayworld.basemodule.common.editrecord.service.LimsEditRecordService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.context.LocalContextHelper;
import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.mybatis.page.PageRowBounds;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.BeanUtils;
import com.sunwayworld.framework.utils.StringUtils;
import com.sunwayworld.module.sys.code.bean.CoreCodeBean;
import com.sunwayworld.module.sys.code.service.CoreCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsQualitycontrolTestResultServiceImpl implements LimsQualitycontrolTestResultService {

    @Autowired
    private LimsQualitycontrolTestResultDao limsQualitycontrolTestResultDao;

    @Autowired
    private LimsQualitycontrolplanProjectService limsQualitycontrolplanProjectService;

    @Autowired
    private LimsQualitycontrolplanService limsQualitycontrolplanService;

    @Autowired
    private LimsQualitycontrolTestResultManageService limsQualitycontrolTestResultManageService;

    @Autowired
    private LimsQualityRuleService limsQualityRuleService;

    @Autowired
    private CoreCodeService coreCodeService;

    @Autowired
    private LimsEditRecordService limsEditRecordService;

    @Autowired
    private LimsGroupService limsGroupService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsQualitycontrolTestResultDao getDao() {
        return limsQualitycontrolTestResultDao;
    }

    final private int XValue = 4;

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsQualitycontrolTestResultBean limsQualitycontrolTestResult = jsonWrapper.parseUnique(LimsQualitycontrolTestResultBean.class);
        limsQualitycontrolTestResult.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsQualitycontrolTestResult);
        return limsQualitycontrolTestResult.getId();
    }

    @Override
    public String insertTestResult(RestJsonWrapperBean jsonWrapperBean) {
        List<Long> planIds = jsonWrapperBean.getBodyList().get(0).stream().map(JSON::parseObject).collect(Collectors.toList()).stream().map(i -> i.getLong("id")).collect(Collectors.toList());
        if (planIds.size() > 0) {
            LimsQualitycontrolplanBean qualitycontrolplanBean = limsQualitycontrolplanService.selectById(planIds.get(0));

            if (qualitycontrolplanBean != null) {
                List<JSONObject> jsonObjects = jsonWrapperBean.getBodyList().get(2).stream().map(JSON::parseObject).collect(Collectors.toList());
//                Long controlTestBatchId = jsonObjects.get(0).getLong("controlTestBatchId");
//                if (controlTestBatchId != null) {
                List<Long> qualitycontrolplanProjectIds = jsonWrapperBean.getBodyList().get(1).stream().map(JSON::parseObject).collect(Collectors.toList()).stream().map(i -> i.getLong("id")).collect(Collectors.toList());

                List<LimsQualitycontrolplanProjectBean> qualitycontrolplanProjectBeans = limsQualitycontrolplanProjectService.selectListByIds(qualitycontrolplanProjectIds).stream().filter(i -> i.getPlanId().equals(qualitycontrolplanBean.getId())).collect(Collectors.toList());

                qualitycontrolplanProjectBeans.forEach(item -> {
                    LimsQualitycontrolTestResultBean resultBean = new LimsQualitycontrolTestResultBean();
                    resultBean.setId(ApplicationContextHelper.getNextIdentity());

//                        resultBean.setControlTestBatchId(controlTestBatchId);
                    resultBean.setControlPlanId(qualitycontrolplanBean.getId());
                    resultBean.setControlProjectId(item.getId());
                    resultBean.setIdentifyCode(qualitycontrolplanBean.getIdentifyCode());

                    resultBean.setControlMaterialName(qualitycontrolplanBean.getControlMaterialName());
                    resultBean.setMainBatchCode(qualitycontrolplanBean.getMainBatchCode());
                    resultBean.setControlMaterialLevel(qualitycontrolplanBean.getControlMaterialLevel());
                    resultBean.setEquipmentName(qualitycontrolplanBean.getEquipmentName());
                    resultBean.setProfessionalGroup(qualitycontrolplanBean.getProfessionalGroup());

                    resultBean.setProductCode(item.getProductCode());
                    resultBean.setProductName(item.getProductName());
                    resultBean.setMethodDesc(item.getMethodDesc());

                    resultBean.setTargetValue(item.getTargetValue());
                    resultBean.setStandardDeviation(item.getStandardDeviation());
                    resultBean.setCvValue(item.getCvValue());
                    resultBean.setControlRule(item.getControlRule());

                    this.getDao().insert(resultBean);
                });
//                }
            }
        }

        return null;
    }

    @Override
    public JSONObject getChosableProfessionalGroup(RestJsonWrapperBean jsonWrapperBean) {
        List<JSONObject> dateFilter = jsonWrapperBean.getBodyList().get(0).stream().map(JSON::parseObject).collect(Collectors.toList());
        JSONObject req = new JSONObject();
        JSONArray professionalGroupsArray = new JSONArray();
        JSONArray equipNamesArray = new JSONArray();

        List<LimsGroupBean> groupBeans = limsGroupService.selectAll();

        if (dateFilter.size() > 0) {
            JSONObject jsonObject = dateFilter.get(0);
            LocalDateTime fromDate = jsonObject.getObject("fromDate", LocalDateTime.class);
            LocalDateTime toDate = jsonObject.getObject("toDate", LocalDateTime.class);
            List<LimsQualitycontrolTestResultBean> resultBeans = this.selectListByFilter(SearchFilter.instance().match("resultSaveTime", fromDate).filter(MatchPattern.TGOE).match("resultSaveTime", toDate).filter(MatchPattern.TLOE).match("status", "1").filter(MatchPattern.EQ));

            Set<String> professionalGroups = new HashSet<>();
            Set<String> equipNames = new HashSet<>();

            resultBeans.forEach(item -> {
                professionalGroups.add(item.getProfessionalGroup());
                equipNames.add(item.getEquipmentName());
            });

            professionalGroups.forEach(value -> {
                Long groupId = null;
                try {
                    groupId = Long.parseLong(value);
                } catch (Exception e) {

                }
                if (groupId != null) {
                    Long finalGroupId = groupId;
                    JSONObject temp = new JSONObject();
                    List<LimsGroupBean> filter = groupBeans.stream().filter(g -> Objects.equals(g.getId(), finalGroupId)).collect(Collectors.toList());
                    if (!filter.isEmpty()) {
                        temp.put("text", filter.get(0).getGroupname());
                        temp.put("value", filter.get(0).getId());
                        professionalGroupsArray.add(temp);
                    }
                }
            });

            equipNames.forEach(value -> {
                JSONObject temp = new JSONObject();
                temp.put("text", value);
                temp.put("value", value);
                equipNamesArray.add(temp);
            });
        }
        req.put("professionalGroups", professionalGroupsArray);
        req.put("equipNames", equipNamesArray);
        return req;
    }

    @Override
    public Page<LimsQualitycontrolTestResultBean> selectDistinctRows(RestJsonWrapperBean wrapper) {
        MapperParameter parameter = wrapper.extractMapFilter();
        PageRowBounds rowBounds = wrapper.extractPageRowBounds();
        return this.selectDistinctRows_(parameter, rowBounds);
    }

    Page<LimsQualitycontrolTestResultBean> selectDistinctRows_(MapperParameter parameter, PageRowBounds rowBounds) {
        return this.selectPagination(() -> this.getDao().selectDistinctRows(parameter), rowBounds);
    }

    @Override
    public JSONObject saveToManageTable(RestJsonWrapperBean wrapper) {
        List<LimsQualitycontrolTestResultBean> resultBeans = wrapper.parse(LimsQualitycontrolTestResultBean.class);
        resultBeans = resultBeans.stream().filter(i -> i.getMainBatchCode() != null).collect(Collectors.toList());
        JSONObject extraData = wrapper.getBodyList().get(1).stream().map(JSON::parseObject).collect(Collectors.toList()).get(0);

        LocalDateTime fromDate = extraData.getObject("fromDate", LocalDateTime.class);
        LocalDateTime toDate = extraData.getObject("toDate", LocalDateTime.class);
        String professionalGroup = extraData.getString("professionalGroup");
        String equipmentName = extraData.getString("equipName");
        Boolean ifKickAbnomalData = extraData.getBoolean("ifKickAbnomalData");
        String abnormalType = extraData.getString("abnormalType");

        final Long groupByCreaterAndId = ApplicationContextHelper.getNextSequenceNumber("T_LIMS_QUALITYCONTROL_TEST_RESULT_MANAGE_GROUPID");// 每次调用后递增，12位的长数字
        String s = ApplicationContextHelper.getConstantValue("T_LIMS_QUALITYCONTROL_TEST_RESULT_MANAGE");
        Double Y = 1d;
        if (!StringUtils.isEmpty(s)) {
            try {
                Y = Double.parseDouble(s);
            } catch (Exception e) {
                Y = 1d;
            }
        }

        Double finalY = Y;
        JSONObject reqData = new JSONObject();
        DecimalFormat decimalFormat = new DecimalFormat("#.000");
//        decimalFormat.setRoundingMode(RoundingMode.CEILING);// 保留三位小数时，末位始终+1
        decimalFormat.setRoundingMode(RoundingMode.HALF_UP);// 保留三位小数时，四舍五入

        List<LimsQualitycontrolTestResultManageBean> insertList = new ArrayList<>();
        Map<LimsQualitycontrolTestResultBean, List<LimsQualitycontrolTestResultManageBean>> insertManageBeanListGroup = new ConcurrentHashMap<>();
        resultBeans.forEach(i -> {
            List<LimsQualitycontrolTestResultBean> resultBeanList = selectListByFilter(SearchFilter.instance().match("resultSaveTime", fromDate).filter(MatchPattern.TGOE).match("resultSaveTime", toDate).filter(MatchPattern.TLOE).match("status", "1").filter(MatchPattern.EQ).match("professionalGroup", professionalGroup).filter(MatchPattern.EQ).match("equipmentName", equipmentName).filter(MatchPattern.EQ).match("mainBatchCode", i.getMainBatchCode()).filter(MatchPattern.EQ).match("controlMaterialLevel", i.getControlMaterialLevel()).filter(MatchPattern.EQ).match("productName", i.getProductName()).filter(MatchPattern.EQ));

            Collections.sort(resultBeanList);

            List<LimsQualitycontrolTestResultBean> filterByRemoveStatus = resultBeanList.stream().filter(k -> StringUtils.isBlank(k.getRemoveStatus()) || "0".equals(k.getRemoveStatus())).collect(Collectors.toList());

/*            AtomicReference<Double> sumResult = new AtomicReference<>(0d);
            resultBeanList.forEach(o -> {
                sumResult.updateAndGet(v -> v + o.getTestResult());
            });
            final Double averageForResult = sumResult.get() / resultBeanList.size();// 平均值
            AtomicReference<Double> temp = new AtomicReference<>(0d);
            resultBeanList.forEach(o -> {
                temp.updateAndGet(v -> v + Math.pow(o.getTestResult() - averageForResult, 2));
            });
            final Double standardDeviationForResult = Math.sqrt(temp.get() / resultBeanList.size());// 标准差*/

            AtomicReference<Double> sumResult = new AtomicReference<>(0d);
            filterByRemoveStatus.forEach(o -> {
                sumResult.updateAndGet(v -> v + o.getTestResult());
            });
            final Double averageForResult = sumResult.get() / filterByRemoveStatus.size();// 平均值
            AtomicReference<Double> temp = new AtomicReference<>(0d);
            filterByRemoveStatus.forEach(o -> {
                temp.updateAndGet(v -> v + Math.pow(o.getTestResult() - averageForResult, 2));
            });
            final Double standardDeviationForResult = Math.sqrt(temp.get() / filterByRemoveStatus.size());// 标准差

            List<LimsQualitycontrolTestResultManageBean> inserManageBeanList = new ArrayList<>();
            resultBeanList.forEach(item -> {
                LimsQualitycontrolTestResultManageBean insertResultManageBean = new LimsQualitycontrolTestResultManageBean();

                BeanUtils.copyProperties(item, insertResultManageBean);

                insertResultManageBean.setResultId(item.getId());// 存放质控结果表的id

                insertResultManageBean.setId(ApplicationContextHelper.getNextIdentity());

                if (StringUtils.isBlank(item.getRemoveStatus()) || "0".equals(item.getRemoveStatus())) {
                    insertResultManageBean.setAverageForResult(Double.valueOf(decimalFormat.format(averageForResult)));
                    insertResultManageBean.setStandardDeviationForResult(Double.valueOf(decimalFormat.format(standardDeviationForResult)));

//                if (standardDeviationForResult == null || standardDeviationForResult == 0d) {
//                    //标准差有可能是0，比如单条数据或者多条全都一样，这里就不放值了
//                    //标准差为0  则该组数据均为相同结果值或者只有一条数据，此时CV设置成0，然后相对值设置成4（图表上展示为x轴上的一条直线)  20220425  zhoujq
//                    //insertResultManageBean.setCvForResult(Double.valueOf(decimalFormat.format(averageForResult / standardDeviationForResult)));
//                    insertResultManageBean.setRelativeValue(4d);
//                    insertResultManageBean.setCvForResult(0d);
//                } else {
//                    //分组结果CV值（平均值/标准差）
////                    insertResultManageBean.setCvForResult(Double.valueOf(decimalFormat.format(averageForResult / standardDeviationForResult)));
//                    insertResultManageBean.setCvForResult(standardDeviationForResult / Double.valueOf(decimalFormat.format(averageForResult)));
//                    //计算相对值
//                    Double relativeValue = finalY + Double.valueOf(decimalFormat.format((insertResultManageBean.getTestResult() - (averageForResult - 3 * standardDeviationForResult)) / standardDeviationForResult));
//                    insertResultManageBean.setRelativeValue(relativeValue);
//                }

                    if (standardDeviationForResult == null || standardDeviationForResult == 0d) {
                        //标准差有可能是0，比如单条数据或者多条全都一样，这里就不放值了
                        insertResultManageBean.setCvForResult(0d);
                    } else {
                        //分组结果CV值（平均值/标准差）
                        insertResultManageBean.setCvForResult(Double.valueOf(decimalFormat.format((standardDeviationForResult / averageForResult) * 100d)));
                    }

                    //------2022-05-16 计算相对值选用的SD改为设定的SD
                    if (item.getStandardDeviation() == null || item.getStandardDeviation() == 0d) {
                        //标准差有可能是0，比如单条数据或者多条全都一样，这里就不放值了
                        //标准差为0  则该组数据均为相同结果值或者只有一条数据，此时CV设置成0，然后相对值设置成4（图表上展示为x轴上的一条直线)  20220425  zhoujq
                        insertResultManageBean.setRelativeValue(4d);
                    } else {//计算相对值
                        Double relativeValue = finalY + Double.valueOf(decimalFormat.format((insertResultManageBean.getTestResult() - (averageForResult - 3 * item.getStandardDeviation())) / item.getStandardDeviation()));
                        insertResultManageBean.setRelativeValue(relativeValue);
                    }
                }

                insertResultManageBean.setGroupByCreaterAndId(groupByCreaterAndId);// 用户每点一次查询会生成一次，用于区分选择的数据，以最新一次数据为展示

                JSONObject obj = new JSONObject();

                CoreCodeBean coreCodeBean = coreCodeService.selectFirstByFilter(SearchFilter.instance().match("codeCategoryId", "controlMaterialLevel").filter(MatchPattern.EQ).match("code", i.getControlMaterialLevel()).filter(MatchPattern.EQ));

                obj.put("name", i.getMainBatchCode() + "-" + (coreCodeBean == null ? i.getControlMaterialLevel() : coreCodeBean.getCodeName()) + "-" + i.getProductName());
                obj.put("dateTime", String.format("%d-%d", insertResultManageBean.getResultSaveTime().getMonthValue(), insertResultManageBean.getResultSaveTime().getDayOfMonth()));
                obj.put("description", "R=" + insertResultManageBean.getTestResult() + ";X=" + String.format("%.3f", insertResultManageBean.getAverageForResult()) + ";SD=" + String.format("%.3f", insertResultManageBean.getStandardDeviationForResult()));
                obj.put("observation", insertResultManageBean.getRelativeValue());
                obj.put("isNormal", true);//异常标志位  默认为true  影响图表上的展示
                insertResultManageBean.setDescribeJSON(obj);

                inserManageBeanList.add(insertResultManageBean);
            });
//            insertManageBeanListGroup.put(i, inserManageBeanList);
            insertManageBeanListGroup.put(i, inserManageBeanList.stream().filter(k -> (StringUtils.isBlank(k.getRemoveStatus())) || ("0".equals(k.getRemoveStatus()))).collect(Collectors.toList()));
            insertList.addAll(inserManageBeanList);
        });
        setAbnormalDesc(insertManageBeanListGroup);

        //ifKickAbnomalData  标识位  前台查询时如果点击的是去除异常点查询，则去掉所有异常点后再入库、拼接JSON
        //由于每次点击都是重新插入数据，所以入不入库应该没啥关系
        if (ifKickAbnomalData != null && ifKickAbnomalData == true) {
            if (abnormalType != null) {
                List<Long> ruleIds = Arrays.stream(abnormalType.split(",")).map(Long::parseLong).collect(Collectors.toList());
                List<LimsQualityRuleBean> kickAbnormalPointRules = limsQualityRuleService.selectListByFilter(SearchFilter.instance().match("id", ruleIds).filter(MatchPattern.OR));
                String sorts = StringUtils.join(kickAbnormalPointRules.stream().map(LimsQualityRuleBean::getSort).collect(Collectors.toList()), ",");
                insertManageBeanListGroup.forEach((k, v) -> {
                    Iterator<LimsQualitycontrolTestResultManageBean> iterator = v.iterator();
                    while (iterator.hasNext()) {
                        LimsQualitycontrolTestResultManageBean bean = iterator.next();
                        if (bean.getAbnormalDesc() != null) {
                            bean.getDescribeJSON().put("description", String.format("%s,%s", bean.getDescribeJSON().getString("description"), bean.getAbnormalDesc()));
                        }
                        if (bean.getDescribeJSON().getString("abnormalSort") != null) {
                            if (sorts.contains(bean.getDescribeJSON().getString("abnormalSort"))) {
                                iterator.remove();
                                insertList.remove(bean);
                            }
                        }
                    }
//                    insertList.addAll(v);
                });
            } else {
                insertManageBeanListGroup.forEach((k, v) -> {
                    Iterator<LimsQualitycontrolTestResultManageBean> iterator = v.iterator();
                    while (iterator.hasNext()) {
                        LimsQualitycontrolTestResultManageBean bean = iterator.next();
                        if (bean.getDescribeJSON().getString("abnormalSort") != null) {
                            iterator.remove();
                            insertList.remove(bean);
                        }
                    }
//                    insertList.addAll(v);
                });
            }
        } else {
            insertManageBeanListGroup.forEach((k, v) -> {
                v.forEach(m -> {
                    if (m.getAbnormalDesc() != null) {
                        m.getDescribeJSON().put("description", String.format("%s,%s", m.getDescribeJSON().getString("description"), m.getAbnormalDesc()));
                    }
                });
//                insertList.addAll(v);
            });
        }

        limsQualitycontrolTestResultManageService.getDao().insert(insertList);

        JSONArray sourceList = new JSONArray();
        insertManageBeanListGroup.forEach((k, v) -> {
            JSONArray tempArray = new JSONArray();

            v.forEach(item -> {
                if (null != item.getDescribeJSON()) {
                    tempArray.add(item.getDescribeJSON());
                }
            });

            if (!tempArray.isEmpty()) {
                if (sourceList.isEmpty()) {
                    sourceList.add(tempArray);
                } else {
                    if (tempArray.size() > sourceList.getJSONArray(0).size()) {
                        sourceList.add(0, tempArray);
                    } else {
                        sourceList.add(tempArray);
                    }
                }
            }
        });

        reqData.put("sourceList", sourceList);
        return reqData;
    }

    public void setAbnormalDesc(Map<LimsQualitycontrolTestResultBean, List<LimsQualitycontrolTestResultManageBean>> inserManageBeanListGroup) {
        Set<LimsQualitycontrolTestResultBean> keys = inserManageBeanListGroup.keySet();
        //List<CoreCodeBean> controlMaterialLevelCodes = coreCodeService.selectListByFilter(SearchFilter.instance().match("codeCategoryId", "controlMaterialLevel").filter(MatchPattern.EQ));
        inserManageBeanListGroup.forEach((k, v) -> {
            if (!v.isEmpty()) {
                String controlRule = v.get(0).getControlRule();
                if (!StringUtils.isBlank(controlRule)) {
                    List<String> controlRuleIds = Arrays.asList(controlRule.split(","));
                    List<LimsQualityRuleBean> ruleBeanList = limsQualityRuleService.selectListByFilter(SearchFilter.instance().match("id", controlRuleIds).filter(MatchPattern.OR));
                    AtomicBoolean has1_2S = new AtomicBoolean(false);
                    if (!ruleBeanList.isEmpty()) {
                        Collections.sort(ruleBeanList);
                        ruleBeanList.forEach(r -> {
                            int N = Integer.parseInt(r.getN());
                            int M = Integer.parseInt(r.getM());
                            int SD = Integer.parseInt(r.getSd());

                            switch (r.getSort()) {
                                case "1": {
                                    //1-2S
                                    v.forEach(item -> {
                                        if (null == item.getDescribeJSON().getString("abnormalSort")) {
                                            if (Math.abs(item.getRelativeValue() - XValue) >= SD && Math.abs(item.getRelativeValue() - XValue) < (SD + 1)) {
                                                //item.getDescribeJSON().put("description", String.format("%s (%d-%dS警告点)", item.getDescribeJSON().getString("description"), M, SD));
                                                item.getDescribeJSON().put("isNormal", false);//异常标志位
                                                //item.getDescribeJSON().put("abnormalSort", "1");
                                                item.setAbnormalDesc(String.format("(%d-%dS警告点)", M, SD));
                                            }
                                        }
                                    });
                                }
                                has1_2S.set(true);
                                break;
                                case "2": {
                                    //1-3S
                                    v.forEach(item -> {
                                        if (null == item.getDescribeJSON().getString("abnormalSort")) {
                                            if (Math.abs(item.getRelativeValue() - XValue) >= SD) {
                                                //item.getDescribeJSON().put("description", String.format("%s (%d-%dS异常点)", item.getDescribeJSON().getString("description"), M, SD));
                                                item.getDescribeJSON().put("isNormal", false);//异常标志位
                                                item.getDescribeJSON().put("abnormalSort", "2");
                                                item.setAbnormalDesc(String.format("(%d-%dS异常点)", M, SD));
                                            }
                                        }
                                    });
                                }
                                break;
                                case "3": {
                                    //2-2S 第一种情况
                                    if (v.size() >= 2) {
                                        for (int i = 0; i < v.size() - 1; i++) {
                                            if (null == v.get(i).getDescribeJSON().getString("abnormalSort")) {
                                                if (Math.abs(v.get(i).getRelativeValue() - XValue) > 2) {
                                                    if (Math.abs(v.get(i + 1).getRelativeValue() - XValue) > 2) {
                                                        if ((v.get(i + 1).getRelativeValue() - XValue) * (v.get(i).getRelativeValue() - XValue) > 0) {
                                                            if (!v.get(i).getDescribeJSON().getString("description").contains(String.format("(%d-%dS异常点)", M, SD))) {
                                                                //v.get(i).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(i).getDescribeJSON().getString("description"), M, SD));
                                                                v.get(i).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                v.get(i).getDescribeJSON().put("abnormalSort", "3");
                                                                v.get(i).setAbnormalDesc(String.format("(%d-%dS异常点)", M, SD));
                                                            }
                                                            //v.get(i + 1).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(i + 1).getDescribeJSON().getString("description"), M, SD));
                                                            v.get(i + 1).getDescribeJSON().put("isNormal", false);//异常标志位
                                                            v.get(i + 1).getDescribeJSON().put("abnormalSort", "3");
                                                            v.get(i + 1).setAbnormalDesc(String.format("(%d-%dS异常点)", M, SD));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //2-2S 第二种情况
//                                    if (k.getControlMaterialLevel().equals(controlMaterialLevelCodes.get(0).getCode())) {
                                    List<LimsQualitycontrolTestResultBean> otherLevelKeys = keys.stream().filter(f -> f.getMainBatchCode().equals(k.getMainBatchCode()) && !f.getControlMaterialLevel().equals(k.getControlMaterialLevel()) && f.getProductName().equals(k.getProductName())).collect(Collectors.toList());
                                    if (otherLevelKeys.size() <= 0) {
                                        break;
                                    } else {
                                        for (int index = 0; index < otherLevelKeys.size(); index++) {
                                            LimsQualitycontrolTestResultBean otherLevelKey = otherLevelKeys.get(index);
                                            List<LimsQualitycontrolTestResultManageBean> otherBeans = inserManageBeanListGroup.get(otherLevelKey);
                                            if (!otherBeans.isEmpty() && !otherBeans.get(0).getControlMaterialLevel().equals(v.get(0).getControlMaterialLevel())) {
                                                v.forEach(v1 -> {
                                                    if (null == v1.getDescribeJSON().getString("abnormalSort")) {
                                                        if (Math.abs(v1.getRelativeValue() - XValue) > SD) {
                                                            List<LimsQualitycontrolTestResultManageBean> filterByTime = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v1.getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v1.getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v1.getResultSaveTime().getDayOfMonth())).collect(Collectors.toList());
                                                            filterByTime.forEach(v2 -> {
                                                                if (Math.abs(v2.getRelativeValue() - XValue) > SD && ((v1.getRelativeValue() - XValue) * (v2.getRelativeValue() - XValue) > 0)) {
                                                                    if (null == v1.getDescribeJSON().getString("abnormalSort")) {
                                                                        //v1.getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v1.getDescribeJSON().getString("description"), M, SD));
                                                                        v1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                        v1.getDescribeJSON().put("abnormalSort", "3");
                                                                        v1.setAbnormalDesc(String.format("(%d-%dS异常点)", M, SD));
                                                                        if (null == v2.getDescribeJSON().getString("abnormalSort")) {
                                                                            //v2.getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v1.getDescribeJSON().getString("description"), M, SD));
                                                                            v2.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                            v2.getDescribeJSON().put("abnormalSort", "3");
                                                                            v2.setAbnormalDesc(String.format("(%d-%dS异常点)", M, SD));
                                                                        }
                                                                    }
                                                                }
                                                            });
                                                        }
                                                    }
                                                });
                                            }
                                        }
                                    }
                                }
//                                }
                                break;
                                case "4": {
                                    //R-4S
//                                   // if (k.getControlMaterialLevel().equals(controlMaterialLevelCodes.get(0).getCode())) {
                                    List<LimsQualitycontrolTestResultBean> otherLevelKeys = keys.stream().filter(f -> f.getMainBatchCode().equals(k.getMainBatchCode()) && !f.getControlMaterialLevel().equals(k.getControlMaterialLevel()) && f.getProductName().equals(k.getProductName())).collect(Collectors.toList());
                                    if (otherLevelKeys.size() <= 0) {
                                        break;
                                    } else {
                                        for (int index = 0; index < otherLevelKeys.size(); index++) {
                                            LimsQualitycontrolTestResultBean otherLevelKey = otherLevelKeys.get(index);
                                            List<LimsQualitycontrolTestResultManageBean> otherBeans = inserManageBeanListGroup.get(otherLevelKey);
                                            v.forEach(v1 -> {
                                                if (null == v1.getDescribeJSON().getString("abnormalSort"))
                                                    if (Math.abs(v1.getRelativeValue() - XValue) > 2) {
                                                        List<LimsQualitycontrolTestResultManageBean> filterByTime = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v1.getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v1.getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v1.getResultSaveTime().getDayOfMonth())).collect(Collectors.toList());
                                                        filterByTime.forEach(v2 -> {
                                                            if (Math.abs(v2.getRelativeValue() - XValue) > 2 && (v1.getRelativeValue() - XValue) * (v2.getRelativeValue() - XValue) < 0) {
                                                                if (null == v1.getDescribeJSON().getString("abnormalSort")) {
                                                                    //v1.getDescribeJSON().put("description", String.format("%s,(R-%dS异常点)", v1.getDescribeJSON().getString("description"), 4));
                                                                    v1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v1.getDescribeJSON().put("abnormalSort", "4");
                                                                    v1.setAbnormalDesc(String.format("(R-%dS异常点)", 4));
                                                                    if (null == v2.getDescribeJSON().getString("abnormalSort")) {
                                                                        //v2.getDescribeJSON().put("description", String.format("%s,(R-%dS异常点)", v1.getDescribeJSON().getString("description"), 4));
                                                                        v2.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                        v2.getDescribeJSON().put("abnormalSort", "4");
                                                                        v2.setAbnormalDesc(String.format("(R-%dS异常点)", 4));
                                                                    }
                                                                }
                                                            }
                                                        });
                                                    }
                                            });
                                        }
                                    }
                                }
//                                }
                                break;
                                case "5": {
                                    if (v.size() < 2) {
                                        break;
                                    } else {
                                        //4-1S 第二种情况
                                        List<LimsQualitycontrolTestResultBean> otherLevelKeys = keys.stream().filter(f -> f.getMainBatchCode().equals(k.getMainBatchCode()) && !f.getControlMaterialLevel().equals(k.getControlMaterialLevel()) && f.getProductName().equals(k.getProductName())).collect(Collectors.toList());
                                        if (otherLevelKeys.size() <= 0) {
                                        } else {
                                            for (int index = 0; index < otherLevelKeys.size(); index++) {
                                                LimsQualitycontrolTestResultBean otherLevelKey = otherLevelKeys.get(index);
                                                List<LimsQualitycontrolTestResultManageBean> otherBeans = inserManageBeanListGroup.get(otherLevelKey);
                                                if (!otherBeans.isEmpty() /*&& !v.get(0).getControlMaterialLevel().equals(otherBeans.get(0).getControlMaterialLevel())*/) {
                                                    for (int g = 0; g <= v.size() - 2; g++) {
                                                        if (null == v.get(g).getDescribeJSON().getString("abnormalSort")) {
                                                            if ((v.get(g).getRelativeValue() - XValue > 1) && (v.get(g + 1).getRelativeValue() - XValue > 1)) {
                                                                int finalG = g;
                                                                List<LimsQualitycontrolTestResultManageBean> filterByTime = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalG).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalG).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalG).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 1)).collect(Collectors.toList());
                                                                List<LimsQualitycontrolTestResultManageBean> filterByTime2 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalG + 1).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalG + 1).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalG + 1).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 1)).collect(Collectors.toList());
                                                                if (filterByTime.size() > 0 && filterByTime2.size() > 0) {
                                                                    if (!has1_2S.get() || (has1_2S.get() && (v.get(g).getRelativeValue() - XValue > 2 || v.get(g + 1).getRelativeValue() - XValue > 2 || !filterByTime.stream().filter(f -> f.getRelativeValue() - XValue > 2).collect(Collectors.toList()).isEmpty() || !filterByTime2.stream().filter(f -> f.getRelativeValue() - XValue > 2).collect(Collectors.toList()).isEmpty()))) {
                                                                        v.get(finalG).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                        v.get(finalG).getDescribeJSON().put("abnormalSort", "5");
                                                                        v.get(finalG).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                        v.get(finalG + 1).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                        v.get(finalG + 1).getDescribeJSON().put("abnormalSort", "5");
                                                                        v.get(finalG + 1).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                        filterByTime.forEach(f1 -> {
                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                        });

                                                                        filterByTime2.forEach(f1 -> {
                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                        });

                                                                        if (g + 3 < v.size() - 1) {
                                                                            for (int j = 1; j < v.size() - 3 - g; j++) {
                                                                                if (v.get(g + 3 + j).getRelativeValue() - XValue > 1) {

                                                                                    int finalJ = j;
                                                                                    int finalG1 = g;
                                                                                    List<LimsQualitycontrolTestResultManageBean> filterByTimeTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalG1 + 3 + finalJ).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalG1 + 3 + finalJ).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalG1 + 3 + finalJ).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 1)).collect(Collectors.toList());
                                                                                    if (filterByTimeTemp.size() > 0) {
                                                                                        v.get(g + 3 + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                        v.get(g + 3 + j).getDescribeJSON().put("abnormalSort", "5");
                                                                                        v.get(g + 3 + j).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                                        filterByTimeTemp.forEach(f1 -> {
                                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                                        });
                                                                                    }
                                                                                } else {
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }

                                                                        if (g > 0) {
                                                                            for (int l = g - 1; l >= 0; l--) {
                                                                                if (v.get(l).getRelativeValue() - XValue > 1 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                                    int finalL = l;
                                                                                    List<LimsQualitycontrolTestResultManageBean> filterByTimeTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalL).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalL).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalL).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 1)).collect(Collectors.toList());
                                                                                    if (filterByTimeTemp.size() > 0) {
                                                                                        v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                        v.get(l).getDescribeJSON().put("abnormalSort", "5");
                                                                                        v.get(l).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                                        filterByTimeTemp.forEach(f1 -> {
                                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                                        });
                                                                                    }
                                                                                } else {
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            } else if (v.get(g).getRelativeValue() - XValue < -1 && v.get(g + 1).getRelativeValue() - XValue < -1) {
                                                                int finalG = g;
                                                                List<LimsQualitycontrolTestResultManageBean> filterByTime = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalG).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalG).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalG).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < -1)).collect(Collectors.toList());
                                                                List<LimsQualitycontrolTestResultManageBean> filterByTime2 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalG + 1).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalG + 1).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalG + 1).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < -1)).collect(Collectors.toList());
                                                                if (filterByTime.size() > 0 && filterByTime2.size() > 0) {
                                                                    if (!has1_2S.get() || (has1_2S.get() && (v.get(g).getRelativeValue() - XValue < -2 || v.get(g + 1).getRelativeValue() - XValue < -2 || !filterByTime.stream().filter(f -> f.getRelativeValue() - XValue < -2).collect(Collectors.toList()).isEmpty() || !filterByTime2.stream().filter(f -> f.getRelativeValue() - XValue < -2).collect(Collectors.toList()).isEmpty()))) {
                                                                        v.get(finalG).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                        v.get(finalG).getDescribeJSON().put("abnormalSort", "5");
                                                                        v.get(finalG).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                        v.get(finalG + 1).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                        v.get(finalG + 1).getDescribeJSON().put("abnormalSort", "5");
                                                                        v.get(finalG + 1).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                        filterByTime.forEach(f1 -> {
                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                        });

                                                                        filterByTime2.forEach(f1 -> {
                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                        });

                                                                        if (g + 3 < v.size() - 1) {
                                                                            for (int j = 1; j < v.size() - 3 - g; j++) {
                                                                                if (v.get(g + 3 + j).getRelativeValue() - XValue < -1) {

                                                                                    int finalJ = j;
                                                                                    int finalG1 = g;
                                                                                    List<LimsQualitycontrolTestResultManageBean> filterByTimeTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalG1 + 3 + finalJ).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalG1 + 3 + finalJ).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalG1 + 3 + finalJ).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < -1)).collect(Collectors.toList());
                                                                                    if (filterByTimeTemp.size() > 0) {
                                                                                        v.get(g + 3 + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                        v.get(g + 3 + j).getDescribeJSON().put("abnormalSort", "5");
                                                                                        v.get(g + 3 + j).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                                        filterByTimeTemp.forEach(f1 -> {
                                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                                        });
                                                                                    }
                                                                                } else {
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }

                                                                        if (g > 0) {
                                                                            for (int l = g - 1; l >= 0; l--) {
                                                                                if (v.get(l).getRelativeValue() - XValue < -1 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                                    int finalL = l;
                                                                                    List<LimsQualitycontrolTestResultManageBean> filterByTimeTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalL).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalL).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalL).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < -1)).collect(Collectors.toList());
                                                                                    if (filterByTimeTemp.size() > 0) {
                                                                                        v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                        v.get(l).getDescribeJSON().put("abnormalSort", "5");
                                                                                        v.get(l).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                                        filterByTimeTemp.forEach(f1 -> {
                                                                                            f1.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            f1.getDescribeJSON().put("abnormalSort", "5");
                                                                                            f1.setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                                        });
                                                                                    }
                                                                                } else {
                                                                                    break;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (v.size() < 4) break;
                                    else {
                                        //4-1S 第一种情况
                                        for (int g = 0; g <= v.size() - 4; g++) {
                                            if (v.get(g).getDescribeJSON().getString("abnormalSort") == null) {
                                                if (v.get(g).getRelativeValue() - XValue > 1) {
                                                    if (v.get(g + 1).getRelativeValue() - XValue > 1) {
                                                        if (v.get(g + 2).getRelativeValue() - XValue > 1) {
                                                            if (v.get(g + 3).getRelativeValue() - XValue > 1) {
                                                                if (!has1_2S.get() || (has1_2S.get() && (v.get(g).getRelativeValue() - XValue > 2 || v.get(g + 1).getRelativeValue() - XValue > 2 || v.get(g + 2).getRelativeValue() - XValue > 2 || v.get(g + 3).getRelativeValue() - XValue > 2))) {
                                                                    //v.get(g).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    //v.get(g + 1).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g + 1).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g + 1).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g + 1).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    //v.get(g + 2).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g + 2).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g + 2).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g + 2).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    //v.get(g + 3).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g + 3).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g + 3).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g + 3).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    if (g + 3 < v.size() - 1) {
                                                                        for (int j = 1; j < v.size() - 3 - g; j++) {
                                                                            if (v.get(g + 3 + j).getRelativeValue() - XValue > 1) {
                                                                                //v.get(g + 3 + j).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                                v.get(g + 3 + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                v.get(g + 3 + j).getDescribeJSON().put("abnormalSort", "5");
                                                                                v.get(g + 3 + j).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                            } else {
                                                                                break;
                                                                            }
                                                                        }
                                                                    }

                                                                    if (g > 0) {
                                                                        for (int l = g - 1; l >= 0; l--) {
                                                                            if (v.get(l).getRelativeValue() - XValue > 1 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                                v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                v.get(l).getDescribeJSON().put("abnormalSort", "5");
                                                                                v.get(l).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                            } else {
                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                } else if (v.get(g).getRelativeValue() - XValue < -1) {
                                                    if (v.get(g + 1).getRelativeValue() - XValue < -1) {
                                                        if (v.get(g + 2).getRelativeValue() - XValue < -1) {
                                                            if (v.get(g + 3).getRelativeValue() - XValue < -1) {
                                                                if (!has1_2S.get() || (has1_2S.get() && (v.get(g).getRelativeValue() - XValue < -2 || v.get(g + 1).getRelativeValue() - XValue < -2 || v.get(g + 2).getRelativeValue() - XValue < -2 || v.get(g + 3).getRelativeValue() - XValue < -2))) {
                                                                    //v.get(g).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    //v.get(g + 1).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g + 1).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g + 1).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g + 1).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    //v.get(g + 2).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g + 2).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g + 2).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g + 2).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    //v.get(g + 3).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                    v.get(g + 3).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(g + 3).getDescribeJSON().put("abnormalSort", "5");
                                                                    v.get(g + 3).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));

                                                                    if (g + 3 < v.size() - 1) {
                                                                        for (int j = 1; j < v.size() - 3 - g; j++) {
                                                                            if (v.get(g + 3 + j).getRelativeValue() - XValue < -1) {
                                                                                //v.get(g + 3 + j).getDescribeJSON().put("description", String.format("%s,(%d-%dS异常点)", v.get(g).getDescribeJSON().getString("description"), 4, 1));
                                                                                v.get(g + 3 + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                v.get(g + 3 + j).getDescribeJSON().put("abnormalSort", "5");
                                                                                v.get(g + 3 + j).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                            } else {
                                                                                break;
                                                                            }
                                                                        }
                                                                    }

                                                                    if (g > 0) {
                                                                        for (int l = g - 1; l >= 0; l--) {
                                                                            if (v.get(l).getRelativeValue() - XValue < -1 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                                v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                v.get(l).getDescribeJSON().put("abnormalSort", "5");
                                                                                v.get(l).setAbnormalDesc(String.format("(%d-%dS异常点)", 4, 1));
                                                                            } else {
                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                                case "6": {

                                    //10-XS 第2种情况
                                    if (v.size() < 5) {
                                        break;
                                    } else {
                                        List<LimsQualitycontrolTestResultBean> otherLevelKeys = keys.stream().filter(f -> f.getMainBatchCode().equals(k.getMainBatchCode()) && !f.getControlMaterialLevel().equals(k.getControlMaterialLevel()) && f.getProductName().equals(k.getProductName())).collect(Collectors.toList());
                                        if (otherLevelKeys.size() <= 0) {
                                        } else {
                                            for (int index = 0; index < otherLevelKeys.size(); index++) {
                                                LimsQualitycontrolTestResultBean otherLevelKey = otherLevelKeys.get(index);
                                                List<LimsQualitycontrolTestResultManageBean> otherBeans = inserManageBeanListGroup.get(otherLevelKey);
                                                if (!otherBeans.isEmpty() /*&& !v.get(0).getControlMaterialLevel().equals(otherBeans.get(0).getControlMaterialLevel())*/) {
                                                    for (int g = 0; g <= v.size() - 5; g++) {
                                                        if (v.get(g).getDescribeJSON().getString("abnormalSort") == null) {

                                                            //H和L都连续>0 5次
                                                            if (v.get(g).getRelativeValue() - XValue > 0) {
                                                                boolean flag = true;
                                                                boolean moreThan2 = false;
                                                                for (int j = 0; j < 5; j++) {
                                                                    if (v.get(g + j).getRelativeValue() - XValue < 0) {
                                                                        flag = false;
                                                                        break;
                                                                    } else {
                                                                        if (v.get(g + j).getRelativeValue() - XValue > 2) {
                                                                            moreThan2 = true;
                                                                        }
                                                                    }
                                                                }
                                                                if (flag) {
                                                                    int g2 = g;
                                                                    List<LimsQualitycontrolTestResultManageBean> filter1 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter2 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 1).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 1).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 1).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter3 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 2).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 2).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 2).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter4 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 3).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 3).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 3).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter5 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 4).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 4).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 4).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 0)).collect(Collectors.toList());

                                                                    if (filter1.size() > 0 && filter2.size() > 0 && filter3.size() > 0 && filter4.size() > 0 && filter5.size() > 0) {
                                                                        if (!has1_2S.get() || (has1_2S.get() && (moreThan2 || !filter1.stream().filter(o -> o.getRelativeValue() - XValue > 2).collect(Collectors.toList()).isEmpty() || !filter2.stream().filter(o -> o.getRelativeValue() - XValue > 2).collect(Collectors.toList()).isEmpty() || !filter3.stream().filter(o -> o.getRelativeValue() - XValue > 2).collect(Collectors.toList()).isEmpty() || !filter4.stream().filter(o -> o.getRelativeValue() - XValue > 2).collect(Collectors.toList()).isEmpty() || !filter5.stream().filter(o -> o.getRelativeValue() - XValue > 2).collect(Collectors.toList()).isEmpty()))) {
                                                                            for (int j = 0; j < 5; j++) {
                                                                                v.get(g + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                v.get(g + j).getDescribeJSON().put("abnormalSort", "6");
                                                                                v.get(g + j).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            }
                                                                            filter1.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter2.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter3.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter4.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter5.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });

                                                                            if (g + 4 < v.size() - 1) {
                                                                                for (int h = 1; h < v.size() - g - 5; h++) {
                                                                                    if (v.get(g + 4 + h).getRelativeValue() - XValue > 0) {
                                                                                        int finalH = h;
                                                                                        List<LimsQualitycontrolTestResultManageBean> filterTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 4 + finalH).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 4 + finalH).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 4 + finalH).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 0)).collect(Collectors.toList());
                                                                                        if (filterTemp.size() > 0) {
                                                                                            v.get(g + 4 + h).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            v.get(g + 4 + h).getDescribeJSON().put("abnormalSort", "6");
                                                                                            v.get(g + 4 + h).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        }
                                                                                        filterTemp.forEach(s -> {
                                                                                            s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            s.getDescribeJSON().put("abnormalSort", "6");
                                                                                            s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        });
                                                                                    } else {
                                                                                        break;
                                                                                    }
                                                                                }
                                                                            }

                                                                            if (g > 0) {
                                                                                for (int l = g - 1; l >= 0; l--) {
                                                                                    if (v.get(l).getRelativeValue() - XValue > 0 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                                        int finalL = l;
                                                                                        List<LimsQualitycontrolTestResultManageBean> filterTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalL).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalL).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalL).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue > 0)).collect(Collectors.toList());
                                                                                        if (filterTemp.size() > 0) {
                                                                                            v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            v.get(l).getDescribeJSON().put("abnormalSort", "6");
                                                                                            v.get(l).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        }
                                                                                        filterTemp.forEach(s -> {
                                                                                            s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            s.getDescribeJSON().put("abnormalSort", "6");
                                                                                            s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        });
                                                                                    } else {
                                                                                        break;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }

                                                            } else if (v.get(g).getRelativeValue() - XValue < 0) {
                                                                boolean flag = true;
                                                                boolean moreThan2 = false;
                                                                for (int j = 0; j < 5; j++) {
                                                                    if (v.get(g + j).getRelativeValue() - XValue > 0) {
                                                                        flag = false;
                                                                        break;
                                                                    } else {
                                                                        if (v.get(g + j).getRelativeValue() - XValue < -2) {
                                                                            moreThan2 = true;
                                                                        }
                                                                    }
                                                                }
                                                                if (flag) {
                                                                    int g2 = g;
                                                                    List<LimsQualitycontrolTestResultManageBean> filter1 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter2 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 1).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 1).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 1).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter3 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 2).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 2).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 2).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter4 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 3).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 3).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 3).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < 0)).collect(Collectors.toList());
                                                                    List<LimsQualitycontrolTestResultManageBean> filter5 = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 4).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 4).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 4).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < 0)).collect(Collectors.toList());

                                                                    if (filter1.size() > 0 && filter2.size() > 0 && filter3.size() > 0 && filter4.size() > 0 && filter5.size() > 0) {
                                                                        if (!has1_2S.get() || (has1_2S.get() && (moreThan2 || !filter1.stream().filter(o -> o.getRelativeValue() - XValue < -2).collect(Collectors.toList()).isEmpty() || !filter2.stream().filter(o -> o.getRelativeValue() - XValue < -2).collect(Collectors.toList()).isEmpty() || !filter3.stream().filter(o -> o.getRelativeValue() - XValue < -2).collect(Collectors.toList()).isEmpty() || !filter4.stream().filter(o -> o.getRelativeValue() - XValue < -2).collect(Collectors.toList()).isEmpty() || !filter5.stream().filter(o -> o.getRelativeValue() - XValue < -2).collect(Collectors.toList()).isEmpty()))) {
                                                                            for (int j = 0; j < 5; j++) {
                                                                                v.get(g + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                v.get(g + j).getDescribeJSON().put("abnormalSort", "6");
                                                                                v.get(g + j).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            }
                                                                            filter1.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter2.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter3.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter4.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });
                                                                            filter5.forEach(s -> {
                                                                                s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                s.getDescribeJSON().put("abnormalSort", "6");
                                                                                s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                            });

                                                                            if (g + 4 < v.size() - 1) {
                                                                                for (int h = 1; h < v.size() - g - 5; h++) {
                                                                                    if (v.get(g + 4 + h).getRelativeValue() - XValue < 0) {
                                                                                        int finalH = h;
                                                                                        List<LimsQualitycontrolTestResultManageBean> filterTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(g2 + 4 + finalH).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(g2 + 4 + finalH).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(g2 + 4 + finalH).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < 0)).collect(Collectors.toList());
                                                                                        if (filterTemp.size() > 0) {
                                                                                            v.get(g + 4 + h).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            v.get(g + 4 + h).getDescribeJSON().put("abnormalSort", "6");
                                                                                            v.get(g + 4 + h).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        }
                                                                                        filterTemp.forEach(s -> {
                                                                                            s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            s.getDescribeJSON().put("abnormalSort", "6");
                                                                                            s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        });
                                                                                    } else {
                                                                                        break;
                                                                                    }
                                                                                }
                                                                            }

                                                                            if (g > 0) {
                                                                                for (int l = g - 1; l >= 0; l--) {
                                                                                    if (v.get(l).getRelativeValue() - XValue < 0 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                                        int finalL = l;
                                                                                        List<LimsQualitycontrolTestResultManageBean> filterTemp = otherBeans.stream().filter(o -> (o.getResultSaveTime().getYear() == v.get(finalL).getResultSaveTime().getYear()) && (o.getResultSaveTime().getMonthValue() == v.get(finalL).getResultSaveTime().getMonthValue()) && (o.getResultSaveTime().getDayOfMonth() == v.get(finalL).getResultSaveTime().getDayOfMonth()) && (o.getRelativeValue() - XValue < 0)).collect(Collectors.toList());
                                                                                        if (filterTemp.size() > 0) {
                                                                                            v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            v.get(l).getDescribeJSON().put("abnormalSort", "6");
                                                                                            v.get(l).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        }
                                                                                        filterTemp.forEach(s -> {
                                                                                            s.getDescribeJSON().put("isNormal", false);//异常标志位
                                                                                            s.getDescribeJSON().put("abnormalSort", "6");
                                                                                            s.setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                                        });
                                                                                    } else {
                                                                                        break;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //10-XS 第一种情况
                                    if (v.size() >= 10) {
                                        for (int g = 0; g <= v.size() - 10; g++) {
                                            //|| v.get(g).getDescribeJSON().getString("abnormalSort").equals(String.format("(%d-XS异常点)", 10))
                                            if (v.get(g).getDescribeJSON().getString("abnormalSort") == null || v.get(g).getDescribeJSON().getString("abnormalSort").equals("6")) {
                                                if (v.get(g).getRelativeValue() - XValue > 0) {
                                                    boolean flag = true;
                                                    boolean moreThan2 = false;
                                                    for (int j = 0; j <= 9; j++) {
                                                        if (v.get(g + j).getRelativeValue() - XValue > 0) {
                                                            if (v.get(g + j).getRelativeValue() - XValue > 2) {
                                                                moreThan2 = true;
                                                            }
                                                        } else {
                                                            flag = false;
                                                        }
                                                    }
                                                    if (flag && (!has1_2S.get() || (has1_2S.get() && (moreThan2)))) {
                                                        for (int j = 0; j <= 9; j++) {
                                                            //v.get(g + j).getDescribeJSON().put("description", String.format("%s,(%d-XS异常点)", v.get(g).getDescribeJSON().getString("description"), 10));
                                                            v.get(g + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                            v.get(g + j).getDescribeJSON().put("abnormalSort", "6");
                                                            v.get(g + j).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                        }

                                                        for (int j = 1; j < v.size() - 9 - g; j++) {
                                                            if (v.get(g + 9 + j).getRelativeValue() - XValue > 0) {
                                                                //v.get(g + 9 + j).getDescribeJSON().put("description", String.format("%s,(%d-XS异常点)", v.get(g).getDescribeJSON().getString("description"), 10));
                                                                v.get(g + 9 + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                v.get(g + 9 + j).getDescribeJSON().put("abnormalSort", "6");
                                                                v.get(g + 9 + j).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                            } else {
                                                                break;
                                                            }
                                                        }

                                                        if (g > 0) {
                                                            for (int l = g - 1; l >= 0; l--) {
                                                                if (v.get(l).getRelativeValue() - XValue > 0 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                    v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(l).getDescribeJSON().put("abnormalSort", "6");
                                                                    v.get(l).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                } else {
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                } else if (v.get(g).getRelativeValue() - XValue < 0) {
                                                    boolean flag = true;
                                                    boolean moreThan2 = false;
                                                    for (int j = 0; j <= 9; j++) {
                                                        if (v.get(g + j).getRelativeValue() - XValue < 0) {
                                                            if (v.get(g + j).getRelativeValue() - XValue < -2) {
                                                                moreThan2 = true;
                                                            }
                                                            continue;
                                                        } else {
                                                            flag = false;
                                                            break;
                                                        }
                                                    }
                                                    if (flag && (!has1_2S.get() || (has1_2S.get() && moreThan2))) {
                                                        for (int j = 0; j <= 9; j++) {
                                                            //v.get(g + j).getDescribeJSON().put("description", String.format("%s,(%d-XS异常点)", v.get(g).getDescribeJSON().getString("description"), 10));
                                                            v.get(g + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                            v.get(g + j).getDescribeJSON().put("abnormalSort", "6");
                                                            v.get(g + j).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                        }

                                                        for (int j = 1; j < v.size() - 9 - g; j++) {
                                                            if (v.get(g + 9 + j).getRelativeValue() - XValue < 0) {
                                                                //v.get(g + 9 + j).getDescribeJSON().put("description", String.format("%s,(%d-XS异常点)", v.get(g).getDescribeJSON().getString("description"), 10));
                                                                v.get(g + 9 + j).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                v.get(g + 9 + j).getDescribeJSON().put("abnormalSort", "6");
                                                                v.get(g + 9 + j).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                            } else {
                                                                break;
                                                            }
                                                        }

                                                        if (g > 0) {
                                                            for (int l = g - 1; l >= 0; l--) {
                                                                if (v.get(l).getRelativeValue() - XValue < 0 && v.get(l).getDescribeJSON().getString("abnormalSort") == null) {
                                                                    v.get(l).getDescribeJSON().put("isNormal", false);//异常标志位
                                                                    v.get(l).getDescribeJSON().put("abnormalSort", "6");
                                                                    v.get(l).setAbnormalDesc(String.format("(%d-XS异常点)", 10));
                                                                } else {
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        });
                    }
                }
            }
        });
    }

    /**
     * 修改质控结果保存修改记录
     *
     * @param jsonWrapperBean
     * @return
     */
    @Override
    public JSONObject updateTestResult(RestJsonWrapperBean jsonWrapperBean) {
        JSONObject req = new JSONObject();

        String oldValue = jsonWrapperBean.getParamValue("oldValue");
        String field = jsonWrapperBean.getParamValue("field");
        LocalDateTime now = LocalDateTime.now();
        LimsQualitycontrolTestResultBean updateBean = jsonWrapperBean.parseUnique(LimsQualitycontrolTestResultBean.class);

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        if (field != null) {
            switch (field) {
                case "testResult":
                case "": {
                    LimsEditRecordBean limsEditRecordBean = new LimsEditRecordBean();
                    limsEditRecordBean.setId(ApplicationContextHelper.getNextIdentity());
                    limsEditRecordBean.setOrderTaskId(updateBean.getId());
                    limsEditRecordBean.setEditedById(LocalContextHelper.getLoginUserId());
                    limsEditRecordBean.setEditedByName(LocalContextHelper.getLoginUserName());
                    limsEditRecordBean.setEditedTime(now);

                    limsEditRecordBean.setFromValue(oldValue);

                    limsEditRecordBean.setFieldName(field);// 存放field名称

                    limsEditRecordBean.setToValue(updateBean.getTestResult().toString());

                    updateBean.setResultSaveTime(now);
                    getDao().update(updateBean, "testResult", "resultSaveTime");
                    limsEditRecordService.getDao().insert(limsEditRecordBean);
                }
                break;
                case "targetValue": {
                    LimsEditRecordBean limsEditRecordBean = new LimsEditRecordBean();
                    limsEditRecordBean.setId(ApplicationContextHelper.getNextIdentity());
                    limsEditRecordBean.setOrderTaskId(updateBean.getId());
                    limsEditRecordBean.setEditedById(LocalContextHelper.getLoginUserId());
                    limsEditRecordBean.setEditedByName(LocalContextHelper.getLoginUserName());
                    limsEditRecordBean.setEditedTime(now);

                    limsEditRecordBean.setFromValue(oldValue);

                    limsEditRecordBean.setFieldName(field);// 存放field名称

                    limsEditRecordBean.setToValue(updateBean.getTargetValue().toString());

                    getDao().update(updateBean, "targetValue");
                    limsEditRecordService.getDao().insert(limsEditRecordBean);
                }
                break;
                case "standardDeviation": {
                    LimsEditRecordBean limsEditRecordBean = new LimsEditRecordBean();
                    limsEditRecordBean.setId(ApplicationContextHelper.getNextIdentity());
                    limsEditRecordBean.setOrderTaskId(updateBean.getId());
                    limsEditRecordBean.setEditedById(LocalContextHelper.getLoginUserId());
                    limsEditRecordBean.setEditedByName(LocalContextHelper.getLoginUserName());
                    limsEditRecordBean.setEditedTime(now);

                    limsEditRecordBean.setFromValue(oldValue);

                    limsEditRecordBean.setFieldName(field);// 存放field名称

                    limsEditRecordBean.setToValue(updateBean.getStandardDeviation().toString());

                    getDao().update(updateBean, "standardDeviation");
                    limsEditRecordService.getDao().insert(limsEditRecordBean);
                }
                break;
                case "cvValue": {
                    LimsEditRecordBean limsEditRecordBean = new LimsEditRecordBean();
                    limsEditRecordBean.setId(ApplicationContextHelper.getNextIdentity());
                    limsEditRecordBean.setOrderTaskId(updateBean.getId());
                    limsEditRecordBean.setEditedById(LocalContextHelper.getLoginUserId());
                    limsEditRecordBean.setEditedByName(LocalContextHelper.getLoginUserName());
                    limsEditRecordBean.setEditedTime(now);

                    limsEditRecordBean.setFromValue(oldValue);

                    limsEditRecordBean.setFieldName(field);// 存放field名称

                    limsEditRecordBean.setToValue(updateBean.getCvValue().toString());

                    getDao().update(updateBean, "cvValue");
                    limsEditRecordService.getDao().insert(limsEditRecordBean);
                }
                break;
                case "resultSaveTime": {
                    LimsEditRecordBean limsEditRecordBean = new LimsEditRecordBean();
                    limsEditRecordBean.setId(ApplicationContextHelper.getNextIdentity());
                    limsEditRecordBean.setOrderTaskId(updateBean.getId());
                    limsEditRecordBean.setEditedById(LocalContextHelper.getLoginUserId());
                    limsEditRecordBean.setEditedByName(LocalContextHelper.getLoginUserName());
                    limsEditRecordBean.setEditedTime(now);

                    limsEditRecordBean.setFromValue(oldValue);

                    limsEditRecordBean.setFieldName(field);// 存放field名称

                    limsEditRecordBean.setToValue(dateTimeFormatter.format(updateBean.getResultSaveTime()));

                    getDao().update(updateBean, "resultSaveTime");
                    limsEditRecordService.getDao().insert(limsEditRecordBean);
                }
                break;
            }
        }

        return req;
    }

    @Override
    public double selectAverageForHistory(String mainBatchCode, String controlMaterialLevel, String productName) {
        return getDao().selectAverageForHistory(mainBatchCode, controlMaterialLevel, productName);
    }

    @Override
    public double selectSDForHistory(String mainBatchCode, String controlMaterialLevel, String productName) {
        return getDao().selectSDForHistory(mainBatchCode, controlMaterialLevel, productName);
    }

    @Override
    public JSONObject submit(RestJsonWrapperBean wrapperBean) {
        JSONObject req = new JSONObject();
        List<LimsQualitycontrolTestResultBean> resultBeans = wrapperBean.parse(LimsQualitycontrolTestResultBean.class);
        List<Long> ids = resultBeans.stream().map(LimsQualitycontrolTestResultBean::getId).collect(Collectors.toList());
        if (ids.isEmpty()) {
            return req;
        }
        LimsQualitycontrolTestResultBean update = new LimsQualitycontrolTestResultBean();
        update.setStatus("1");
        update.setSubmitTime(LocalDateTime.now());
        update.setSubmitById(LocalContextHelper.getLoginUserId());
        update.setSubmitByName(LocalContextHelper.getLoginUserName());
        getDao().updateByIds(update, ids, "status", "submitById", "submitByName", "submitTime");
        return req;
    }

    @Override
    public JSONObject back(RestJsonWrapperBean wrapperBean) {
        JSONObject req = new JSONObject();
        List<LimsQualitycontrolTestResultBean> resultBeans = wrapperBean.parse(LimsQualitycontrolTestResultBean.class);
        List<Long> ids = resultBeans.stream().map(LimsQualitycontrolTestResultBean::getId).collect(Collectors.toList());
        if (ids.isEmpty()) {
            return req;
        }
        LimsQualitycontrolTestResultBean update = new LimsQualitycontrolTestResultBean();
        update.setStatus("0");
        getDao().updateByIds(update, ids, "status");
        return req;
    }
}
